package org.desktop.web.client.custom.form;

import org.desktop.web.client.DesktopViewer;
import org.desktop.web.client.util.JavascriptUtil;
import org.desktop.web.client.util.Util;
import org.desktop.web.shared.util.WebService;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.googlecode.gwt.crypto.bouncycastle.DataLengthException;
import com.googlecode.gwt.crypto.bouncycastle.InvalidCipherTextException;
import com.googlecode.gwt.crypto.client.TripleDesCipher;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.form.FormPanel;

public class LoginForm extends FormPanel {

	private DesktopViewer container;

	public LoginForm(DesktopViewer container) {
		super();
		this.container = container;
	}

	public void submit() {
		RequestBuilder rb = new RequestBuilder(RequestBuilder.POST,
				GWT.getModuleBaseURL() + "exec?service=" + WebService.AUTH);

		rb.setHeader("Content-Type", "application/x-www-form-urlencoded");
		final DesktopViewer view = container; 
		
		try {
			Util.showWaitCursor();
			rb.sendRequest(getPayload(), new RequestCallback() {

				@Override
				public void onResponseReceived(Request request,
						Response response) {
					JSONValue p = JSONParser.parseStrict(response.getText());
					JSONObject obj = null;
					if ((obj=p.isObject()) != null) {
//						= p.isObject();
						if(obj.get("error")!=null){
							AlertMessageBox amb = new AlertMessageBox("Error", obj.get("error").isString().stringValue());
							amb.show();
							Util.showDefaultCursor();
							return;
						}
						String token = obj.get("token").isString()
								.stringValue();
						Cookies.setCookie("token", token);
						Cookies.setCookie("functions", obj.get("functions").isArray().toString());
						Util.showDefaultCursor();
						Window.Location.assign("http://hrpo:8888/DesktopViewer.html");
						
						
//						Window.alert(DesktopViewer.token);
					}
					Util.showDefaultCursor();
				}

				@Override
				public void onError(Request request, Throwable exception) {
					// TODO Auto-generated method stub
//					Window.alert(exception.getMessage());
				}
			});
		} catch (RequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private String getPayload() {
		String payload = "";
		InputElement username = DOM.getElementById("username-input")
				.<InputElement> cast();
		InputElement pwd = DOM.getElementById("password-input")
				.<InputElement> cast();
		payload = username.getName() + "=" + username.getValue() + "&"
				+ pwd.getName() + "=" +pwd.getValue();
		

		return payload;
	}
}
